<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="/common/commonCss.jsp"></jsp:include>
<jsp:include page="/common/commonJs.jsp"></jsp:include>
<script type="text/javascript"
	src="${basePath }commonJs/quinn_commons.js"></script>
<script src="${basePath }commonJs/jquery.ztree.all.js"></script>
<script>
	function partyLink() {
		var isChecked = $("#patry").attr("checked") == "checked";
		var baidu = "goods.html";
		var sina = "goods_x.html";
		if (isChecked) {
			$("#party_link").attr("href", baidu);
		} else {
			$("#party_link").attr("href", sina);
		}
		$("#party_link")[0].click();
	}
</script>

<SCRIPT type="text/javascript">
	var zNodes = [];

	var rdp_treeNode = null;

	var setting = {
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			onClick : zTreeOnClick
		}
	};

	/*得到商品分类的数据信息*/
	function getTreeData() {
		$
				.ajax({
					url : "${basePath}scmInvocing/productInfo/findGoodsCategoryList.shtml",
					dataType : "json",
					type : "post",
					async : false,
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					},
					success : function(result) {
						var node;
						$(result).each(function(id, item) {
							if (id == 0) {
								node = {
									id : item.pkPrdcateId,
									pId : item.fkParPrdcateId,
									name : item.cateName,
									open : true,
									reserveFlag : item.reserveFlag,
									resverseType : item.resverseType
								};
							} else {
								node = {
									id : item.pkPrdcateId,
									pId : item.fkParPrdcateId,
									name : item.cateName,
									reserveFlag : item.reserveFlag,
									resverseType : item.resverseType
								};
							}
							zNodes.push(node);
						});
					}

				});

	}

	$(document).ready(function() {
		getTreeData();
		$.fn.zTree.init($("#sptree"), setting, zNodes);
		inittab();
		$("#patry").change(function() {
			var patryId = $("#pdId").val();
			var chked = $(this).attr("checked") == "checked";
			$.ajax({
				url : "${basePath}goods/setParty.shtml",
				type : "post",
				dataType : "json",
				data : {
					isParty : chked,
					patryId : patryId
				}
			});
		});
	});

	function changePd() {
		var patryId = $("#pdId").val();
		var chked = $("#patry").attr("checked") == "checked";
		if (chked) {
			$.ajax({
				url : "${basePath}goods/setParty.shtml",
				type : "post",
				dataType : "json",
				data : {
					isParty : chked,
					patryId : patryId
				}
			});
		}
	}

	function inittab() {
		var form = $("#form1");
		var pagebean = new PageBean(form);
		pagebean.setCallback(cb);
		pagebean.setFootId("foot1");
		pagebean.setTableId("table1");
		setPagebeanObj(pagebean);
		pagebean.doPage();
	}

	function jian(id) {
		var v = $("#productCount" + id).val();
		if (parseInt(v) > 0) {
			$("#productCount" + id).val(parseInt(v) - 1);
		}
	}

	function jia(id) {
		var v = $("#productCount" + id).val();
		if (validateCount(id, parseInt(v) + 1)) {
			$("#productCount" + id).val(parseInt(v) + 1);
		}
	}

	function validateCount(id, num) {
		var n = $("#kc_" + id).html();
		if (parseInt(n) < num) {
			alertInfo("库存已不足");
			return false;
		}
		return true;
	}

	function changeCount(id) {
		var n = $("#productCount" + id).val();
		if (isNaN(n)) {
			alertInfo("输入不合法");
			$("#productCount" + id).val(0);
			return;
		}
		if (!validateCount(id, parseInt(n))) {
			$("#productCount" + id).val(0);
		}
	}

	function buy(id) {
		var c = $("#patry").attr("checked") == "checked";
		var p = null;
		if (c) {
			p = $("#pdId").val();
		}

		var shopCartId = $("#shopCartId").val();

		var cou = $("#productCount" + id).val();

		if (isNaN(cou)) {
			alertInfo("数量不合法");
			$("#productCount" + id).val(0);
			return;
		}

		if (parseInt(cou) < 1) {
			alertInfo("购买数量至少为1");
			return;
		}

		var params = {
			isParty : c,
			partyId : p,
			productId : id,
			shopCartId : shopCartId,
			count : cou
		};

		$.ajax({
			url : "${basePath}goods/put2Order.shtml",
			type : "post",
			dataType : "text",
			data : params,
			success : function(data) {
				if (data != null) {
					$("#shopCartId").val(data);
					modifyGoodsStgCnt(id, -parseInt(cou), null);
					var no = $("#kc_" + id).html();
					$("#kc_" + id).html(parseInt(no) - parseInt(cou));
					alertInfo("操作成功");
				}
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}

	function buy2(id, price) {

		var c = $("#patry").attr("checked") == "checked";

		if (c) {
			alertInfo("派对不允许购买礼券");
			return;
		}

		var no = $("#id_txt_" + id).val();
		if (no == null || no.length == 0) {
			alertInfo("礼券编号不能为空");
			return;
		}

		var flag = false;
		$.ajax({
			url : "${basePath}goods/validGiftno.shtml?id=" + no,
			type : "post",
			dataType : "json",
			data : {
				id : no
			},
			async : false,
			success : function(data) {
				flag = data;
			}
		});
		if (!flag) {
			alertInfo("编号已经被使用");
			return;
		}

		$.ajax({
			url : "${basePath}goods/buyGiftNo.shtml",
			type : "post",
			dataType : "json",
			data : {
				id : id,
				no : no,
				price : price
			},
			async : false,
			success : function(data) {
				if (data) {
					alertInfo("购买成功");
					$("#id_txt_" + id).val("");
				}
			}
		});

	}

	function cb(data) {
		$("#ul1 li").remove();
		$
				.each(
						data,
						function(ind, obj) {
							obj = emptyJson(obj);
							var disValue = 1;
							if (obj.disValue != null
									&& parseFloat(obj.disValue) > 0) {
								disValue = obj.disValue;
							}
							var li = "<li><img style=\"width: 176px;height: 176px;\" src=\"${basePath}common/getImageByAttfile.shtml?attfileId="
									+ obj.fkPicFile + "\">";
							li += "<p class=\" color_lv f18 ml10\">￥"
									+ obj.productPrice * disValue + "</p>";
							li += "<p class=\"pl10 goods_so\">" + obj.prdName
									+ "&nbsp;" + obj.prdSpec + " </p>";
							li += "<p class=\"pl10\">库存：<span id=\"kc_"+obj.pkPrdId+"\">"
									+ (obj.stgCnt == "" ? 0 : obj.stgCnt)
									+ "</span></p>";
							li += "<p class=\" p5\">";
							if (obj.resverseType != "20") {
								li += "<a class=\"jia_icon\" href=\"javascript:jian('"
										+ obj.pkPrdId
										+ "')\"></a> <input class=\"iput_t w50\" type=\"text\" value=\"0\" id=\"productCount"
										+ obj.pkPrdId
										+ "\" onchange=\"changeCount('"
										+ obj.pkPrdId + "');\"> ";
								li += "<a class=\"jian_icon\" href=\"javascript:jia('"
										+ obj.pkPrdId + "')\"></a> ";
								li += "<input type=\"button\" value=\"购买\" class=\"btn p010 ml5 w50\" onclick=\"buy('"
										+ obj.pkPrdId + "')\">";
							} else {
								li += "<input class=\"iput_t w100\" type=\"text\" id=\"id_txt_"+obj.pkPrdId+"\">";
								li += "<input type=\"button\" value=\"购买\" class=\"btn p010 ml5 w50\" onclick=\"buy2('"
										+ obj.pkPrdId
										+ "','"
										+ obj.productPrice + "')\">";
							}
							li += "</p></li>";
							$("#ul1").append(li);
						});
	}

	function zTreeOnClick(event, treeId, treeNode) {
		//alert("处理操作事件信息!!");
		rdp_treeNode = treeNode;// 每次点击赋值 
		if (treeNode.id == "001") {
			$("#cateId").val(null);
		} else {
			$("#cateId").val(treeNode.id);
		}
		inittab();
	};

	function search() {
		var pname = $("#seachprd").val();
		$("#prdName").val(pname);
		inittab();
	}

	function goShopcart() {
		var c = $("#patry").attr("checked") == "checked";
		var p = "";
		if (c) {
			var pd = $("#pdId").val();
			$.ajax({
				url : "${basePath}order/getOrderId.shtml",
				type : "post",
				dataType : "text",
				data : {
					pdId : pd
				},
				async : false,
				success : function(data) {
					p = data;
				},
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				}
			});
		} else {
			p = $("#shopCartId").val();
		}
		$("#shop_cart_link").attr("href",
				"${basePath}order/shopCart.shtml?id=" + p);
		$("#shop_cart_link")[0].click();
	}

	function modifyGoodsStgCnt(id, count, suc) {

		$.ajax({
			url : "${basePath}goods/modifyGoodsStgCnt.shtml",
			type : "post",
			dataType : "json",
			data : {
				productId : id,
				count : count,
				orderId : ""
			},
			async : false,
			success : function(data) {
				if (suc != null)
					suc();
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
</SCRIPT>


<title>商品列表</title>
</head>
<body class="bg">
	<table style="display: none;" id="table1">
		<tr></tr>
		<tr></tr>
	</table>
	<form action="${basePath }goods/paginate.shtml" id="form1"
		method="post">
		<input type="hidden" name="pageSize" value="10"> <input
			type="hidden" name="pageNo" value="1"> <input type="hidden"
			name="fkPrdcateId" id="cateId"> <input type="hidden"
			name="prdName" id="prdName">
	</form>
	<input type="hidden" id="shopCartId" value="${shopCartId }">
	<a id="shop_cart_link"></a>
	<div class="content m_auto">
		<div class="content_l mt10">
			<ul>
				<shiro:hasPermission name="goods/list.shtml">
					<li class="content_hover"><a
						href="${basePath }goods/list.shtml"><i class="nav_icon_k"></i><br>商品</a></li>
				</shiro:hasPermission>
				<shiro:hasPermission name="order/shopCart.shtml">
					<li><a href="${basePath }order/shopCart.shtml"><i
							class="nav_icon_l"></i><br> 购物篮 </a></li>
				</shiro:hasPermission>
				<shiro:hasPermission name="order/list.shtml">
					<li><a href="${basePath }order/list.shtml"><i
							class="nav_icon_m"></i><br>订单</a></li>
				</shiro:hasPermission>
			</ul>
		</div>
		<div class="content_r mt10">
			<div class="m10">
				<div class="goods_ware fixed">
					<ul class="goods_ware_ul">
						<li><input type="text" class="iput_t" id="seachprd"><input
							type="button" class="btn ml10" value="搜 索" onclick="search();"></li>
						<li class="f14"><input type="checkbox" class="mr5" id="patry"
							${isParty?'checked':'' }>派对
							<div class="uboxstyle">
								<select name="language_a" id="pdId" onchange="changePd()">
									<c:forEach items="${reserves }" var="re">
										<option value="${re.pkOrderId }"
											${shopCartId eq re.pkOrderId ? 'selected':'' }>${re.subject }</option>
									</c:forEach>
								</select>
							</div></li>
						<a id="party_link" style="display: none"></a>
						<li class="goods_i" onClick="javascript:goShopcart()"
							style="cursor: pointer;"><i class="nav_icon_k goods_m"></i>
							<br> <span>购物篮</span></li>
					</ul>
				</div>
				<div>
					<div class="ztree ztree_z fl" id="sptree"></div>
					<ul class="goods_ware_ulx" id="ul1">
					</ul>
				</div>
				<div class="page fr m10" id="foot1"></div>
			</div>
		</div>
	</div>




</body>
</html>